---
title: CLI Commands
i18nReady: true
---
import Since from '~/components/Since.astro';
import PackageManagerTabs from '~/components/tabs/PackageManagerTabs.astro'
import ReadMore from '~/components/ReadMore.astro'

You can use the Command-Line Interface (CLI) provided by Astro to develop, build, and preview your project from a terminal window.

### `astro` commands

Use the CLI by running one of the **commands** documented on this page with your preferred package manager, optionally followed by any **flags**. Flags customize the behavior of a command.

One of the commands you'll use most often is `astro dev`. This command starts the development server and gives you a live, updating preview of your site in a browser as you work:

<PackageManagerTabs>
  <Fragment slot="npm">
  ```shell
  # start the development server
  npx astro dev
  ```
  </Fragment>
  <Fragment slot="pnpm">
  ```shell
  # start the development server
  pnpm astro dev
  ```
  </Fragment>
  <Fragment slot="yarn">
  ```shell
  # start the development server
  yarn astro dev
  ```
  </Fragment>
</PackageManagerTabs>

You can type `astro --help` in your terminal to display a list of all available commands:

<PackageManagerTabs>
  <Fragment slot="npm">
  ```shell
  npx astro --help
  ```
  </Fragment>
  <Fragment slot="pnpm">
  ```shell
  pnpm astro --help
  ```
  </Fragment>
  <Fragment slot="yarn">
  ```shell
  yarn astro --help
  ```
  </Fragment>
</PackageManagerTabs>

The following message will display in your terminal:

```bash
astro [command] [...flags]

Commands
              add  Add an integration.
            build  Build your project and write it to disk.
            check  Check your project for errors.
              dev  Start the development server.
             docs  Open documentation in your web browser.
             info  List info about your current Astro setup.
          preview  Preview your build locally.
             sync  Generate content collection types.
      preferences  Configure user preferences.
        telemetry  Configure telemetry settings.

Global Flags
  --config <path>  Specify your config file.
    --root <path>  Specify your project root folder.
     --site <url>  Specify your project site.
--base <pathname>  Specify your project base.
        --verbose  Enable verbose logging.
         --silent  Disable all logging.
        --version  Show the version number and exit.
           --open  Open the app in the browser on server start.
           --help  Show this help message.
```

:::note
The extra `--` before any flag is necessary for `npm` to pass your flags to the `astro` command.
:::

### `package.json` scripts

You can also use scripts in `package.json` for shorter versions of these commands. Using a script allows you to use the same commands that you may be familiar with from other projects, such as `npm run build`.

The following scripts for the most common `astro` commands (`astro dev`, `astro build`, and `astro preview`) are added for you automatically when you create a project using [the `create astro` wizard](/en/install/auto/#1-run-the-setup-wizard).

When you follow the instructions to [install Astro manually](/en/install/manual/#2-install-astro), you are instructed to add these scripts yourself. You can also add more scripts to this list manually for any commands you use frequently.

```json title="package.json"
{
  "scripts": {
    "dev": "astro dev",
    "start": "astro dev",
    "build": "astro build",
    "preview": "astro preview"
  }
}
```

You will often use these `astro` commands, or the scripts that run them, without any flags. Add flags to the command when you want to customize the command's behavior. For example, you may wish to start the development server on a different port, or build your site with verbose logs for debugging.

<PackageManagerTabs>
  <Fragment slot="npm">
  ```shell
  # run the dev server on port 8080 using the `start` script in `package.json`
  npm run start -- --port 8080

  # build your site with verbose logs using the `build` script in `package.json`
  npm run build -- --verbose
  ```
  </Fragment>
  <Fragment slot="pnpm">
  ```shell
  # run the dev server on port 8080 using the `start` script in `package.json`
  pnpm start --port 8080

  # build your site with verbose logs using the `build` script in `package.json`
  pnpm build --verbose
  ```
  </Fragment>
  <Fragment slot="yarn">
  ```shell
  # run the dev server on port 8080 using the `start` script in `package.json`
  yarn start --port 8080

  # build your site with verbose logs using the `build` script in `package.json`
  yarn build --verbose
  ```
  </Fragment>
</PackageManagerTabs>

{/*

ORIGINAL CONTENT That We Can Always revert to if new stuff is too friendly

You can use the Command-Line Interface (CLI) provided by Astro to develop, build, and preview your project from a terminal window.

Use the CLI by running one of the **commands** documented on this page, optionally followed by any **flags**. Flags customize the behavior of a command. For example, to start the development server on port `8080`, you would combine the `astro dev` command with the `--port` flag: `astro dev --port 8080`.

In most cases you will use the CLI via your package manager:

<PackageManagerTabs>
  <Fragment slot="npm">
  ```shell
  npx astro dev --port 8080
  ```
  </Fragment>
  <Fragment slot="pnpm">
  ```shell
  pnpm astro dev --port 8080
  ```
  </Fragment>
  <Fragment slot="yarn">
  ```shell
  yarn astro dev --port 8080
  ```
  </Fragment>
</PackageManagerTabs>

If you started your project using [the `create astro` wizard](/en/install/auto/#1-run-the-setup-wizard), you can also use the scripts in `package.json` for a shorter version of these commands. See the `README.md` in your project for details of which commands are available.

<PackageManagerTabs>
  <Fragment slot="npm">
  ```shell
  # run the dev server on port 8080 using the `start` script in `package.json`
  npm run start -- --port 8080
  ```
  </Fragment>
  <Fragment slot="pnpm">
  ```shell
  # run the dev server on port 8080 using the `start` script in `package.json`
  pnpm start --port 8080
  ```
  </Fragment>
  <Fragment slot="yarn">
  ```shell
  # run the dev server on port 8080 using the `start` script in `package.json`
  yarn start --port 8080
  ```
  </Fragment>
</PackageManagerTabs>

*/}

## `astro dev`

Runs Astro's development server. This is a local HTTP server that doesn't bundle assets. It uses Hot Module Replacement (HMR) to update your browser as you save changes in your editor.

## `astro build`

Builds your site for deployment. By default, this will generate static files and place them in a `dist/` directory. If [SSR is enabled](/en/guides/server-side-rendering/), this will generate the necessary server files to serve your site.

Can be combined with the [common flags](#common-flags) documented below.

## `astro preview`

Starts a local server to serve the contents of your static directory (`dist/` by default) created by running `astro build`.

This command allows you to preview your site locally [after building](#astro-build) to catch any errors in your build output before deploying it. It is not designed to be run in production. For help with production hosting, check out our guide on [Deploying an Astro Website](/en/guides/deploy/).

Since Astro 1.5.0, `astro preview` also works for SSR builds if you use an adapter that supports it. Currently, only the [Node adapter](/en/guides/integrations-guide/node/) supports `astro preview`.

Can be combined with the [common flags](#common-flags) documented below.

## `astro check`

Runs diagnostics (such as type-checking within `.astro` files) against your project and reports errors to the console. If any errors are found the process will exit with a code of **1**.

This command is intended to be used in CI workflows.

<h3>Flags</h3>

Use these flags to customize the behavior of the command.

#### `--watch`

The command will watch for any changes in your project, and will report any errors.

<ReadMore>Read more about [type checking in Astro](/en/guides/typescript/#type-checking).</ReadMore>

## `astro sync`

<p><Since v="2.0.0" /></p>

:::tip
Running `astro dev`, `astro build` or `astro check` will run the `sync` command as well.
:::

Generates TypeScript types for all Astro modules. This sets up a [`src/env.d.ts` file](/en/guides/typescript/#setup) for type inferencing, and defines modules for features that rely on generated types:
- The `astro:content` module for the [Content Collections API](/en/guides/content-collections/).
- The `astro:db` module for [Astro DB](/en/guides/astro-db/).

## `astro add`

Adds an integration to your configuration. Read more in [the integrations guide](/en/guides/integrations-guide/#automatic-integration-setup).

## `astro docs`

Launches the Astro Docs website directly from the terminal.

## `astro info`

Reports useful information about your current Astro environment. Useful for providing information when opening an issue.

```shell
astro info
```

Example output:

```
Astro                    v3.0.12
Node                     v20.5.1
System                   macOS (arm64)
Package Manager          pnpm
Output                   server
Adapter                  @astrojs/vercel/serverless
Integrations             none
```

## `astro preferences`

Manage user preferences with the `astro preferences` command. User preferences are specific to individual Astro users, unlike the `astro.config.mjs` file which changes behavior for everyone working on a project.

User preferences are scoped to the current project by default, stored in a local `.astro/settings.json` file.

Using the `--global` flag, user preferences can also be applied to every Astro project on the current machine. Global user preferences are stored in an operating system-specific location.

For example, to disable the devToolbar in a specific Astro project:

```shell
astro preferences disable devToolbar
```

To disable the devToolbar in all Astro projects on the current machine:

```shell
astro preferences disable --global devToolbar
```

The devToolbar can later be enabled with:

```shell
astro preferences enable devToolbar
```

The `reset` command resets a preference to its default value:

```shell
astro preferences reset devToolbar
```

The `list` command prints the current settings of all configurable user preferences. It also supports a machine-readable `--json` output.

```shell
astro preferences list
```

## `astro telemetry`

Sets telemetry configuration for the current CLI user. Telemetry is anonymous data that provides the Astro team insights into which Astro features are most often used. For more information see [Astro's telemetry page](https://astro.build/telemetry/).

Telemetry can be disabled with this CLI command:

```shell
astro telemetry disable
```

Telemetry can later be re-enabled with:

```shell
astro telemetry enable
```

The `reset` command resets the telemetry data:

```shell
astro telemetry reset
```

:::tip[Want to disable telemetry in CI environments?]
Add the `astro telemetry disable` command to your CI scripts or set the `ASTRO_TELEMETRY_DISABLED` environment variable.
:::

## Common flags

### `--root <path>`

Specifies the path to the project root. If not specified, the current working directory is assumed to be the root.

The root is used for finding the Astro configuration file.

```shell
astro --root myRootFolder/myProjectFolder dev
```

### `--config <path>`

Specifies the path to the config file relative to the project root. Defaults to `astro.config.mjs`. Use this if you use a different name for your configuration file or have your config file in another folder.

```shell
astro --config config/astro.config.mjs dev
```

### `--outDir <path>`

<p><Since v="3.3.0" /></p>

Configures the [`outDir`](/en/reference/configuration-reference/#outdir) for your project. Passing this flag will override the `outDir` value in your `astro.config.mjs` file, if one exists.

### `--site <url>`

Configures the [`site`](/en/reference/configuration-reference/#site) for your project. Passing this flag will override the `site` value in your `astro.config.mjs` file, if one exists.

### `--base <pathname>`

<p><Since v="1.4.1" /></p>

Configures the [`base`](/en/reference/configuration-reference/#base) for your project. Passing this flag will override the `base` value in your `astro.config.mjs` file, if one exists.

### `--port <number>`

Specifies which port to run the dev server and preview server on. Defaults to `4321`.

### `--host [optional host address]`

Sets which network IP addresses the dev server and preview server should listen on (i.e. non-localhost IPs). This can be useful for testing your project on local devices like a mobile phone during development.

- `--host` — listen on all addresses, including LAN and public addresses
- `--host <custom-address>` — expose on a network IP address at `<custom-address>`

:::caution
Do not use the `--host` flag to expose the dev server and preview server in a production environment. The servers are designed for local use while developing your site only.
:::

### `--verbose`

Enables verbose logging, which is helpful when debugging an issue.

### `--silent`

Enables silent logging, which will run the server without any console output.

## Global flags

Use these flags to get information about the `astro` CLI.

### `--version`

Prints the Astro version number and exits.

### `--open`

Automatically opens the app in the browser on server start. Can be passed a full URL string (e.g. `--open http://example.com`) or a pathname (e.g. `--open /about`) to specify the URL to open.

### `--help`

Prints the help message and exits.

## Advanced APIs (Experimental)

If you need more control when running Astro, the `"astro"` package also exports APIs to programmatically run the CLI commands.

These APIs are experimental and their API signature may change. Any updates will be mentioned in the [Astro changelog](https://github.com/withastro/astro/blob/main/packages/astro/CHANGELOG.md) and the information below will always show the current, up-to-date information.

### `AstroInlineConfig`

The `AstroInlineConfig` type is used by all of the command APIs below. It extends from the user [Astro config](/en/reference/configuration-reference/) type:

```ts
interface AstroInlineConfig extends AstroUserConfig {
	configFile?: string | false;
	mode?: "development" | "production";
	logLevel?: "debug" | "info" | "warn" | "error" | "silent";
}
```

#### `configFile`

<p>

**Type:** `string | false`<br />
**Default:** `undefined`
</p>

A custom path to the Astro config file.

If this value is undefined (default) or unset, Astro will search for an `astro.config.(js,mjs,ts)` file relative to the `root` and load the config file if found.

If a relative path is set, it will resolve based on the current working directory.

Set to `false` to disable loading any config files.

The inline config passed in this object will take highest priority when merging with the loaded user config.

#### `mode`

<p>

**Type:** `"development" | "production"`<br />
**Default:** `"development"` when running `astro dev`, `"production"` when running `astro build`
</p>

The mode used when building your site to generate either "development" or "production" code.

#### `logLevel`

<p>

**Type:** `"debug" | "info" | "warn" | "error" | "silent"`<br />
**Default:** `"info"`
</p>

The logging level to filter messages logged by Astro.

- `"debug"`: Log everything, including noisy debugging diagnostics.
- `"info"`: Log informational messages, warnings, and errors.
- `"warn"`: Log warnings and errors.
- `"error"`: Log errors only.
- `"silent"`: No logging.

### `dev()`

**Type:** `(inlineConfig: AstroInlineConfig) => AstroDevServer`

Similar to [`astro dev`](#astro-dev), it runs Astro's development server.

```js
import { dev } from "astro";

const devServer = await dev({
  root: "./my-project",
});

// Stop the server if needed
await devServer.stop();
```

### `build()`

**Type:** `(inlineConfig: AstroInlineConfig) => void`

Similar to [`astro build`](#astro-build), it builds your site for deployment.

```js
import { build } from "astro";

await build({
  root: "./my-project",
});
```

### `preview()`

**Type:** `(inlineConfig: AstroInlineConfig) => AstroPreviewServer`

Similar to [`astro preview`](#astro-preview), it starts a local server to serve your static `dist/` directory.

```js
import { preview } from "astro";

const previewServer = await preview({
  root: "./my-project",
});

// Stop the server if needed
await previewServer.stop();
```

### `sync()`

**Type:** `(inlineConfig: AstroInlineConfig) => number`

Similar to [`astro sync`](#astro-sync), it generates TypeScript types for all Astro modules

```js
import { sync } from "astro";

const exitCode = await sync({
  root: "./my-project",
});

process.exit(exitCode)
```

## Astro Studio CLI

### `astro login`

Authenticate with Astro Studio. This is required to run all database management commands, including [`astro link`](#astro-link).

### `astro link`

Link to your Studio hosted database. This is required to run [Astro DB commands](/en/guides/integrations-guide/db/#astro-db-cli-reference) for database management. To link a database, you will be prompted to select an existing database or create a new one.

### `astro logout`

Log out of Astro Studio and remove any locally stored authentication keys.
